package com.lst.tools.basics.serializer;

import com.lst.tools.basics.exception.SerializerException;
import com.lst.tools.basics.utils.DateUtils;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

import java.util.Date;

/**
 * 转换为日期，主要是后台传递给前台时的日期格式（根据泛型不同可对其他类型做处理，如格式化金额）
 *
 * @author: Frank
 * @date: 2019/09/24 13:51
 * @version: V1.0
 */
public class DateJsonSerializer extends JsonSerializer<Date> {

    @Override
    public void serialize(Date date, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) {
        try {
            jsonGenerator.writeString(DateUtils.format(date, DateUtils.DATE_FORMAT_2));
        } catch (Exception e) {
            e.printStackTrace();
            throw new SerializerException("格式化日期异常");
        }
    }
}
